package org.hhchat.hiloCoder.code52;

import sun.font.TrueTypeFont;

import java.util.*;

/**
 * Created this one by huminghao on 2018/3/25.
 */

public class P1 {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        List<String> inputs = new ArrayList<>();
        String dict = scanner.next();
        while (n-- > 0) {
            inputs.add(scanner.next());
        }
        Collections.sort(inputs, new NewString(dict));
        for (String string : inputs) {
            System.out.println(string);
        }
    }

    static class NewString implements Comparator<String>{

        String dict;
        public NewString(String dict) {
            this.dict = dict;
        }

        public int checkDict(char a, char b) {
            return dict.indexOf(a) - dict.indexOf(b);
        }

        @Override
        public int compare(String o1, String o2) {
            int a = 0;
            int b = 0;
            while (a < o1.length() && b < o2.length()) {
                char ac = o1.charAt(a);
                char bc = o2.charAt(b);
                if (checkDict(ac, bc) != 0) {
                    return checkDict(ac, bc);
                }
                a++;
                b++;
            }
            return o1.length() - o2.length();
        }
    }
}
